package com.anzhen.domain.service.impl;

import com.anzhen.domain.dao.UserMapper;
import com.anzhen.domain.entity.User;
import com.anzhen.domain.entity.UserExample;
import com.anzhen.domain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhenan on 2017-6-22.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByEmail(String email) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUEmailEqualTo(email);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.isEmpty()) {
            User user = new User();
            user.setuEmail(email);
            user.setuName(email);
            userMapper.insertSelective(user);
            return user;
        } else {
            return users.get(0);
        }
    }
}
